﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace GFluentCode.Tools
{
    /// <summary>
    /// MD5帮助类
    /// </summary>
    public class Md5Helper
    {
        /// <summary>
        /// 生成MD5串
        /// </summary>
        /// <param name="data">原数据</param>
        /// <returns></returns>
        public static string MakeMd5(string data)
        {
            byte[] d = Encoding.UTF8.GetBytes(data);
            MD5 m = new MD5CryptoServiceProvider();
            byte[] r = m.ComputeHash(d);
            StringBuilder builder = new StringBuilder();
            foreach (byte b in r)
                builder.Append(b.ToString("x2").ToUpper());
            return builder.ToString();
        }

        /// <summary>
        /// 生成MD5串
        /// </summary>
        /// <param name="data">原数据</param>
        /// <returns></returns>
        public static string MakeMd5(string data,Encoding encoding)
        {
            byte[] d = encoding.GetBytes(data);
            MD5 m = new MD5CryptoServiceProvider();
            byte[] r = m.ComputeHash(d);
            StringBuilder builder = new StringBuilder();
            foreach (byte b in r)
                builder.Append(b.ToString("x2").ToUpper());
            return builder.ToString();
        }
    }
}
